#include "multiboot.h"

/*
The layout of the Multiboot header must be as follows:
    Offset	Type	Field Name	Note
    0	u32	magic	required
    4	u32	flags	required
    8	u32	checksum	required
    12	u32	header_addr	if flags[16] is set
    16	u32	load_addr	if flags[16] is set
    20	u32	load_end_addr	if flags[16] is set
    24	u32	bss_end_addr	if flags[16] is set
    28	u32	entry_addr	if flags[16] is set
    32	u32	mode_type	if flags[2] is set
    36	u32	width	if flags[2] is set
    40	u32	height	if flags[2] is set
    44	u32	depth	if flags[2] is set
*/
.code32
.section .multiboot
    .long MB_MAGIC
    .long MB_FLAGS
    .long MB_CHECKSUM(MB_FLAGS)

.section .text
    .global start_
    start_:
            movl $stack_top, %esp
            /*
                TODO
            */
            call _kernel_init

            subl $0x6, %esp
            movl $_gdt, 2(%esp)
            movl $_gdt_limit, %eax
            movw %ax, (%esp)
            lgdt (%esp)
            addl $0x6, %esp

            subl $0x6, %esp
            movl $_idt, 2(%esp)
            movl $_idt_limit, %eax
            movw %ax, (%esp)
            lidt (%esp)
            addl $0x6, %esp

            movw $0x10, %ax // b 0000000000010 | 000
            movw %ax, %es
            movw %ax, %ss
            movw %ax, %fs
            movw %ax, %gs
            movw %ax, %ds

            pushw $0x08         // CS 16 bits
            pushl $_after_gdt   // EIP 32 bits
            retf

        _after_gdt:
            pushl %ebx
            call _kernel_main

            cli     // Abort interruption
        _fin:
            hlt
            jmp _fin


// Uninitialized var
/*
    stk_bot
       |
       \/
    stk_top
*/
.section .bss
    .align 16
    stack_bottom:
        .skip 16384, 0 // 16KB
    stack_top:
        
